﻿namespace TradePublish.Common
{
    using System;
    using System.Management;
    using System.Runtime.InteropServices;
    using System.Text;

    public class SystemInfo
    {
        private const int CHAR_COUNT = 0x80;

        public string GetCpuId()
        {
            ManagementObjectCollection instances = new ManagementClass("Win32_Processor").GetInstances();
            foreach (ManagementObject obj2 in instances)
            {
                return obj2.Properties["ProcessorId"].Value.ToString();
            }
            return null;
        }

        public CpuInfo GetCpuInfo()
        {
            CpuInfo cpuInfo = new CpuInfo();
            GetSystemInfo(ref cpuInfo);
            return cpuInfo;
        }

        public string GetMainHardDiskId()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
            foreach (ManagementObject obj2 in searcher.Get())
            {
                return obj2["SerialNumber"].ToString().Trim();
            }
            return null;
        }

        public MemoryInfo GetMemoryInfo()
        {
            MemoryInfo memInfo = new MemoryInfo();
            GlobalMemoryStatus(ref memInfo);
            return memInfo;
        }

        public string GetOperationSystemInName()
        {
            OperatingSystem oSVersion = Environment.OSVersion;
            string str = "UNKNOWN";
            switch (oSVersion.Platform)
            {
                case PlatformID.Win32Windows:
                    switch (oSVersion.Version.Minor)
                    {
                        case 0:
                            str = "Windows 95";
                            break;

                        case 10:
                            str = "Windows 98";
                            break;

                        case 90:
                            str = "Windows ME";
                            break;
                    }
                    break;

                case PlatformID.Win32NT:
                    switch (oSVersion.Version.Major)
                    {
                        case 3:
                            str = "Windws NT 3.51";
                            break;

                        case 4:
                            str = "Windows NT 4";
                            break;

                        case 5:
                            if (oSVersion.Version.Minor == 0)
                            {
                                str = "Windows 2000";
                            }
                            else if (oSVersion.Version.Minor == 1)
                            {
                                str = "Windows XP";
                            }
                            else if (oSVersion.Version.Minor == 2)
                            {
                                str = "Windows Server 2003";
                            }
                            break;

                        case 6:
                            str = "Longhorn";
                            break;
                    }
                    break;
            }
            return string.Format("{0},{1}", str, oSVersion.Version.ToString());
        }

        public string GetSysDirectory()
        {
            StringBuilder sysDir = new StringBuilder(0x80);
            GetSystemDirectory(sysDir, 0x80);
            return sysDir.ToString();
        }

        [DllImport("kernel32")]
        private static extern void GetSystemDirectory(StringBuilder SysDir, int count);
        [DllImport("kernel32")]
        private static extern void GetSystemInfo(ref CpuInfo cpuInfo);
        [DllImport("kernel32")]
        private static extern void GetSystemTime(ref SystemTimeInfo sysInfo);
        public SystemTimeInfo GetSystemTimeInfo()
        {
            SystemTimeInfo sysInfo = new SystemTimeInfo();
            GetSystemTime(ref sysInfo);
            return sysInfo;
        }

        public string GetWinDirectory()
        {
            StringBuilder winDir = new StringBuilder(0x80);
            GetWindowsDirectory(winDir, 0x80);
            return winDir.ToString();
        }

        [DllImport("kernel32")]
        private static extern void GetWindowsDirectory(StringBuilder WinDir, int count);
        [DllImport("kernel32")]
        private static extern void GlobalMemoryStatus(ref MemoryInfo memInfo);
    }
}

